package com.hyk.commonLib.common.utils.eventHandler;

import android.app.Activity;
import android.view.View;

import androidx.fragment.app.Fragment;

import com.hyk.commonLib.common.activity.BaseActivity;
import com.hyk.commonLib.common.utils.viewModel.BaseObservableModel;

import org.jetbrains.annotations.NotNull;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.ref.SoftReference;

/**
 * @param <CONTAINER> activity/fragment/dialogFragment/view
 * @param <MODEL>
 */
public abstract class BaseEventHandler<CONTAINER, MODEL extends BaseObservableModel> {
    private final SoftReference<CONTAINER> containerReference;
    private final MODEL model;

    BaseEventHandler(@NotNull CONTAINER container, MODEL model) {
        if (!(
                container instanceof Activity ||
                        container instanceof Fragment ||
                        container instanceof View
        )) {
            throw new RuntimeException("BaseEventHandler->CONTAINER 应是 activity/fragment/view");
        }
        this.containerReference = new SoftReference<>(container);
        this.model = model;
    }

    public boolean containerAvailable() {
        return getContainer() != null;
    }

    public CONTAINER getContainer() {
        return containerReference.get();
    }

    public MODEL getModel() {
        return model;
    }

    public abstract BaseActivity<?> getActivity();

    @Retention(RetentionPolicy.RUNTIME)
    @Target(ElementType.METHOD)
    public @interface CheckContainer {
    }
}
